﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Hand : MonoBehaviour
{
    public GameObject dot;
    public LineRenderer rayLine;
    private Ray ray;
    private RaycastHit hit;
    private Transform lastHit;
    private Transform currentHit;

    void Start()
    {
        rayLine.startWidth = 0.003f;
        rayLine.endWidth = 0.0015f;
        ray = new Ray();
        hit = new RaycastHit();
    }

    void Update()
    {
        ray.origin = transform.position;
        ray.direction =transform.forward;

        if (Physics.Raycast(ray, out hit))
        {
            currentHit = hit.transform;
            if (currentHit != null && lastHit != null && currentHit != lastHit)
            {
                if (lastHit.GetComponent<Pvr_UIGraphicRaycaster>() && lastHit.transform.gameObject.activeInHierarchy && lastHit.GetComponent<Pvr_UIGraphicRaycaster>().enabled)
                {
                    lastHit.GetComponent<Pvr_UIGraphicRaycaster>().enabled = false;
                }
            }
            if (currentHit != null && lastHit != null && currentHit == lastHit)
            {
                if (currentHit.GetComponent<Pvr_UIGraphicRaycaster>() && !currentHit.GetComponent<Pvr_UIGraphicRaycaster>().enabled)
                {
                    currentHit.GetComponent<Pvr_UIGraphicRaycaster>().enabled = true;

                }
            }
            lastHit = hit.transform;

            dot.transform.position = hit.point;
        }
        rayLine.SetPosition(0, transform.position);
        rayLine.SetPosition(1, dot.transform.position);
    }
}
